package com.sense.kukoo.common.repo.http

import com.sense.kukoo.common.repo.bean.*
import com.sense.kukoo.common.repo.http.bean.*
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

object KukooHttp {

    private val retrofitApi: KukooHttpApi by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
        KukooHttpManager.instance.createHttpClient(KukooHttpApi::class.java)
    }

    suspend fun indexContent(pageIndex: Int, pageSize: Int, moduleId: Long): ResultBean<List<IndexContentBean>> {
        return retrofitApi.indexContent("${pageIndex}", "${pageSize}", "${moduleId}")
    }

    suspend fun getRecommendData(code: String): ResultBean<List<RecommendBean>> {
        return retrofitApi.recommend(code)
    }

    suspend fun getMoreData(pageIndex: Int, pageSize: Int, code: String): ResultBean<QueryPageBean<BookBean>> {
        return retrofitApi.more("${pageIndex}", "${pageSize}", code)
    }

    suspend fun indexModule(): ResultBean<List<ModuleBean>> {
        return retrofitApi.indexModule()
    }

    suspend fun messageCount(): ResultBean<UserMessagesBean> {
        return retrofitApi.messageCount()
    }

    suspend fun index(): ResultBean<IndexBean> {
        return retrofitApi.index()
    }

    suspend fun searchQueryPage(pageIndex: Int, pageSize: Int): ResultBean<QueryPageBean<SearchRecordBean>> {
        return retrofitApi.searchQueryPage("${pageIndex}", "${pageSize}")
    }

    suspend fun hotWordQueryPage(): ResultBean<QueryPageBean<HotWordBean>> {
        val hashMap = HashMap<String, Any>()
        hashMap.put("pageIndex", 1)
        hashMap.put("pageSize", 10)
        hashMap.put("query", HashMap<String, Any>())
        val mRequestBody: RequestBody = KukooHttpManager.createRequestBody(hashMap)

        return retrofitApi.hotWordQueryPage(mRequestBody)
    }

    suspend fun searchBook(keywords:String, pageIndex: Int, pageSize: Int): ResultBean<QueryPageBean<BookBean>> {
        val hashMap = HashMap<String, Any>()
        hashMap.put("keywords", keywords)
        hashMap.put("pageIndex", pageIndex)
        hashMap.put("pageSize", pageSize)
        val mRequestBody: RequestBody = KukooHttpManager.createRequestBody(hashMap)
        //
        return retrofitApi.searchBook(mRequestBody)
    }

    suspend fun hotWordList(): ResultBean<List<String>> {
        return retrofitApi.hotWordList()
    }

    suspend fun userSearch(
        pageIndex: Int,
        pageSize: Int,
        keyword: String,
    ): ResultBean<List<UserSearchBean>> {
        return retrofitApi.userSearchByKeyword("${pageIndex}", "${pageSize}", keyword)
    }

    suspend fun keywordSearch(
        pageIndex: Int,
        pageSize: Int,
        keyword: String,
    ): ResultBean<List<IndexContentBean>> {
        return retrofitApi.keywordSearch("${pageIndex}", "${pageSize}", keyword)
    }

    suspend fun followSearch(pageIndex: Int, pageSize: Int): ResultBean<List<IndexContentBean>> {
        return retrofitApi.followSearch("${pageIndex}", "${pageSize}")
    }

    suspend fun followListUser(): ResultBean<List<UserSearchBean>> {
        return retrofitApi.followListUser()
    }

    suspend fun getUserContent(
        pageIndex: Int,
        pageSize: Int,
        userId: Long,
    ): ResultBean<List<IndexContentBean>> {
        return retrofitApi.userSearchById("${pageIndex}", "${pageSize}", "${userId}")
    }

    suspend fun subscriptionQueryPage(pageIndex: Int, pageSize: Int): ResultBean<QueryPageBean<LibrarySubscriptionBean>> {
        return retrofitApi.subscriptionQueryPage("${pageIndex}", "${pageSize}")
    }

    suspend fun subscriptionQueryItemPage(pageIndex: Int, pageSize: Int): ResultBean<QueryPageBean<BookItemBean>> {
        return retrofitApi.subscriptionQueryItemPage("${pageIndex}", "${pageSize}")
    }

    suspend fun bookDetails(id: String): ResultBean<BookDetailsBean> {
        return retrofitApi.bookDetails(id)
    }

    suspend fun lightOff(id: String): ResultBean<Boolean> {
        return retrofitApi.lightOff(id)
    }

    suspend fun registerPush(fcmToken: String): ResultBean<Boolean> {
        val hashMap = HashMap<String, Any>()
        hashMap.put("fcmToken", fcmToken)
        val mRequestBody: RequestBody = KukooHttpManager.createRequestBody(hashMap)

        return retrofitApi.registerPush(mRequestBody)
    }

    suspend fun feedback(
        content: String,
        contact: String,
    ): ResultBean<Boolean> {

        val hashMap = HashMap<String, Any>()
        hashMap.put("content", content)
        hashMap.put("contact", contact)
        val mRequestBody: RequestBody = KukooHttpManager.createRequestBody(hashMap)

        return retrofitApi.feedback(mRequestBody)
    }

    suspend fun userSubscriptionPage(isFans: Int, pageIndex:Int, pageSize:Int): ResultBean<List<UserSearchBean>> {
        val hashMap = HashMap<String, Any>()
        hashMap.put("isFans", isFans)//类型: 1/查询粉丝数据 0/查询关注数据
        hashMap.put("pageIndex", pageIndex)
        hashMap.put("pageSize", pageSize)
        val mRequestBody: RequestBody = KukooHttpManager.createRequestBody(hashMap)

        return retrofitApi.userSubscriptionPage(mRequestBody)
    }

    suspend fun subscription(userId: Long): ResultBean<Boolean> {
        return retrofitApi.subscription("${userId}")
    }

    //source : 用户来源: 0. 内部; 1. facebook; 2. google; 3.apple; 4.twitter; 5. line;
    suspend fun thirdLogin(
        uniqueId: String,
        source: Int,
        avatarUrl: String? = null,
        nickname: String? = null,
    ): ResultBean<LoginResultBean> {
        val hashMap = HashMap<String, Any>()
        hashMap.put("uniqueId", uniqueId)
        hashMap.put("source", source)
        avatarUrl?.let {
            hashMap.put("avatarUrl", it)
        }
        nickname?.let {
            hashMap.put("nickname", it)
        }
        val mRequestBody: RequestBody = KukooHttpManager.createRequestBody(hashMap)

        return retrofitApi.thirdLogin(mRequestBody)
    }

    suspend fun removeUser(): ResultBean<Any> {
        return retrofitApi.remove()
    }

    suspend fun uploadPic(file: File): ResultBean<String> {
        val params = HashMap<String, Any>()
        params.put("type", 1)
        val requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file) as RequestBody
        val part = MultipartBody.Part.createFormData("file", file.name, requestFile ) as MultipartBody.Part
        return retrofitApi.uploadPic(params, part)
    }

    suspend fun edit(
        avatarUrl: String?,
        nickname: String?,
    ): ResultBean<Boolean> {

        val hashMap = HashMap<String, Any>()
        if (avatarUrl!=null) {
            hashMap.put("avatarUrl", avatarUrl)
        }
        if (nickname!=null) {
            hashMap.put("channelName", nickname)
        }
        val mRequestBody: RequestBody = KukooHttpManager.createRequestBody(hashMap)

        return retrofitApi.edit(mRequestBody)
    }

    suspend fun sendVerifyCode(
        areaCode: String,
        mobile: String,
    ): ResultBean<Boolean> {
        val hashMap = HashMap<String, Any>()
        hashMap.put("areaCode", areaCode)
        hashMap.put("mobile", mobile)
        val mRequestBody: RequestBody = KukooHttpManager.createRequestBody(hashMap)

        return retrofitApi.sendVerifyCode(mRequestBody)
    }

    suspend fun login(
        account: String,
        areaCode: String? = null,
        password: String? = null,
        verifyCode: String? = null
    ): ResultBean<LoginResultBean> {
        val hashMap = HashMap<String, Any>()
        hashMap.put("account", account)
        areaCode?.let {
            hashMap.put("areaCode", it)
        }
        password?.let {
            hashMap.put("password", it)
        }
        verifyCode?.let {
            hashMap.put("verifyCode", it)
        }
        val mRequestBody: RequestBody = KukooHttpManager.createRequestBody(hashMap)

        return retrofitApi.login(mRequestBody)
    }

    suspend fun getUserInfo(userId: Long): ResultBean<UserInfoBean> {
        return retrofitApi.getUserInfo("${userId}")
    }

    suspend fun isSubscription(
        userId: Long
    ): ResultBean<Boolean> {
        return retrofitApi.isSubscription("${userId}")
    }

    suspend fun commentList(
        contentId: Long,
        parentId: Long,
        pageIndex: Int,
        pageSize: Int,
    ): ResultBean<ArrayList<CommentRecordBean>> {
        val hashMap = HashMap<String, Any>()
        hashMap.put("contentId", contentId)
        if(false){
            hashMap.put("limit1", "limit1")
            hashMap.put("limit2", "limit2")
        }
        hashMap.put("pageIndex", pageIndex)
        hashMap.put("pageSize", pageSize)
        hashMap.put("parentId", parentId)
        val mRequestBody: RequestBody = KukooHttpManager.createRequestBody(hashMap)

        return retrofitApi.commentList(mRequestBody)
    }

    suspend fun commentSave(
        contentId: Long,
        contentUserId: Long,
        content: String,
        replyUserName: String,
        parentId: Long,
        replyUserId: Long,
    ): ResultBean<CommentRecordBean> {
        val hashMap = HashMap<String, Any>()
        hashMap.put("contentId", contentId)
        hashMap.put("content", content)
        hashMap.put("contentUserId", contentUserId)
        hashMap.put("parentId", parentId)
        hashMap.put("replyUserId", replyUserId)
        hashMap.put("replyUserName", replyUserName)
        val mRequestBody: RequestBody = KukooHttpManager.createRequestBody(hashMap)

        return retrofitApi.commentSave(mRequestBody)
    }

    suspend fun commentRemove(id: Long): ResultBean<Boolean> {
        return retrofitApi.commentRemove("${id}")
    }

    suspend fun isLike(contentId: Long): ResultBean<Boolean> {
        return retrofitApi.isLike("${contentId}")
    }

    suspend fun like(contentId: Long, contentUserId: Long): ResultBean<Boolean> {
        return retrofitApi.like("${contentId}", "${contentUserId}")
    }

    suspend fun contentInfo(id: Long): ResultBean<ContentInfoBean> {
        return retrofitApi.contentInfo("${id}")
    }

    suspend fun contentListening(contentId: Long): ResultBean<ContentListeningBean> {
        return retrofitApi.contentListening("${contentId}")
    }

    suspend fun commentSave(
        contentId: Long,
        contentUserId: Long,
        content: String,
        parentId: Long,
        replyUserId: Long,
    ): ResultBean<CommentRecordBean> {
        val hashMap = HashMap<String, Any>()
        hashMap.put("contentId", contentId)
        hashMap.put("content", content)
        hashMap.put("contentUserId", contentUserId)
        hashMap.put("parentId", parentId)
        hashMap.put("replyUserId", replyUserId)
        val mRequestBody: RequestBody = KukooHttpManager.createRequestBody(hashMap)

        return retrofitApi.commentSave(mRequestBody)
    }

    suspend fun shareSuccess(id: Long): ResultBean<Boolean> {
        return retrofitApi.shareSuccess("${id}")
    }

    suspend fun bookQueryPage(
        book_id: Long,
        pageIndex: Int,
        pageSize: Int,
        asc: Int,
    ): ResultBean<QueryPageBean<BookItemBean>> {
        val hashMap = HashMap<String, Any>()
        val sort = HashMap<String, Any>()
        val orders = HashMap<String, Any>()
        val query = HashMap<String, Any>()
        val terms = HashMap<String, Any>()
        hashMap.put("pageIndex", pageIndex)
        hashMap.put("pageSize", pageSize)
        hashMap.put("sort", sort)
        sort.put("order", arrayOf(orders))
        orders.put("asc", asc)//是否正序: 0. 否; 1. 是;
        orders.put("name", "chapter")
        hashMap.put("query", query)
        query.put("terms", arrayOf(terms))
        terms.put("field", "book_id")
        terms.put("value", book_id)
        val mRequestBody: RequestBody = KukooHttpManager.createRequestBody(hashMap)

        return retrofitApi.bookQueryPage(mRequestBody)
    }

    suspend fun nextChapters(
        book_id: Long,
        chapter:Int,
        asc: Int,
    ): ResultBean<QueryPageBean<BookItemBean>> {
        var chapterType = ""
        var newChapter = chapter
        if (asc==1) {
            //正序
            chapterType = "ge"
            newChapter++
        }else{
            //反序
            chapterType = "le"
            newChapter--
        }
        val hashMap = HashMap<String, Any>()
        val sort = HashMap<String, Any>()
        val orders = HashMap<String, Any>()
        val query = HashMap<String, Any>()
        val terms = HashMap<String, Any>()
        val ranges = HashMap<String, Any>()
        hashMap.put("pageIndex", 1)
        hashMap.put("pageSize", 20)
        hashMap.put("sort", sort)
        sort.put("order", arrayOf(orders))
        orders.put("asc", asc)//是否正序: 0. 否; 1. 是;
        orders.put("name", "chapter")
        hashMap.put("query", query)
        query.put("terms", arrayOf(terms))
        terms.put("field", "book_id")
        terms.put("value", book_id)
        query.put("ranges", arrayOf(ranges))
        ranges.put("field", "chapter")
        ranges.put(chapterType, newChapter)
        val mRequestBody: RequestBody = KukooHttpManager.createRequestBody(hashMap)

        return retrofitApi.bookQueryPage(mRequestBody)
    }

    suspend fun subscription(bookId: String): ResultBean<Boolean> {
        return retrofitApi.subscription(bookId)
    }

    suspend fun report(id: String, type: Int): ResultBean<Boolean> {
        val hashMap = HashMap<String, Any>()
        hashMap.put("id", id)
        hashMap.put("type", type)
        val mRequestBody: RequestBody = KukooHttpManager.createRequestBody(hashMap)

        return retrofitApi.report(mRequestBody)
    }

    suspend fun listenPage(
        pageIndex: Int,
        pageSize: Int,
    ): ResultBean<List<ListenRecordBean>> {

        return retrofitApi.listenPage("${pageIndex}", "${pageSize}")
    }

    suspend fun listenDelete(ids: Array<Long>): ResultBean<Boolean> {
        val hashMap = HashMap<String, Any>()
        hashMap.put("ids", ids)
        val mRequestBody: RequestBody = KukooHttpManager.createRequestBody(hashMap)

        return retrofitApi.listenDelete(mRequestBody)
    }

    suspend fun listenClean(): ResultBean<Boolean> {
        return retrofitApi.listenClean()
    }

    suspend fun messageComments(pageIndex: Int, pageSize: Int): ResultBean<ArrayList<UserMessageCommentsBean>> {
        val hashMap = HashMap<String, Any>()
        if(false){
            hashMap.put("currentUserId", "")
            hashMap.put("limit1", "")
            hashMap.put("limit2", "")
        }
        hashMap.put("pageIndex", pageIndex)
        hashMap.put("pageSize", pageSize)
        hashMap.put("type", 3)
        val mRequestBody: RequestBody = KukooHttpManager.createRequestBody(hashMap)

        return retrofitApi.messageComments(mRequestBody)
    }

    suspend fun messageDelete(id: Long?, type: Int?): ResultBean<Boolean> {
        val hashMap = HashMap<String, Any>()
        id?.let {
            hashMap.put("id", it)//删除时 类型为(1,2,3)必传
        }
        type?.let {
            hashMap.put("type", it)//类型: 1/like 2/subscription 3/comment 4/系统消息 5/系统创作者消息
        }
        val mRequestBody: RequestBody = KukooHttpManager.createRequestBody(hashMap)

        return retrofitApi.messageDelete(mRequestBody)
    }

    suspend fun messageFollowers(pageIndex: Int, pageSize: Int): ResultBean<ArrayList<UserMessageFollowersBean>> {
        val hashMap = HashMap<String, Any>()
        if(false){
            hashMap.put("currentUserId", "")
            hashMap.put("limit1", "")
            hashMap.put("limit2", "")
        }
        hashMap.put("pageIndex", pageIndex)
        hashMap.put("pageSize", pageSize)
        hashMap.put("type", 2)
        val mRequestBody: RequestBody = KukooHttpManager.createRequestBody(hashMap)

        return retrofitApi.messageFollowers(mRequestBody)
    }

    suspend fun messageLikes(pageIndex: Int, pageSize: Int): ResultBean<ArrayList<UserMessageLikeBean>> {
        val hashMap = HashMap<String, Any>()
        if(false){
            hashMap.put("currentUserId", "")
            hashMap.put("limit1", "")
            hashMap.put("limit2", "")
        }
        hashMap.put("pageIndex", pageIndex)
        hashMap.put("pageSize", pageSize)
        hashMap.put("type", 1)
        val mRequestBody: RequestBody = KukooHttpManager.createRequestBody(hashMap)

        return retrofitApi.messageLikes(mRequestBody)
    }

    suspend fun messageReddotClean(type: Int): ResultBean<Boolean> {
        val hashMap = HashMap<String, Any>()
        hashMap.put("type", type)
        val mRequestBody: RequestBody = KukooHttpManager.createRequestBody(hashMap)

        return retrofitApi.messageReddotClean(mRequestBody)
    }

    suspend fun messageSystems(pageIndex: Int, pageSize: Int, type: Int): ResultBean<ArrayList<UserMessageSystemBean>> {
        val hashMap = HashMap<String, Any>()
        if(false){
            hashMap.put("currentUserId", "")
            hashMap.put("limit1", "")
            hashMap.put("limit2", "")
        }
        hashMap.put("pageIndex", pageIndex)
        hashMap.put("pageSize", pageSize)
        hashMap.put("type", type)
        val mRequestBody: RequestBody = KukooHttpManager.createRequestBody(hashMap)

        return retrofitApi.messageSystems(mRequestBody)
    }

    suspend fun saveListen(
        contentId: Long,
        contentUserId: Long,
        total: Long,
        isEnding:Boolean
    ): ResultBean<Boolean> {
        val hashMap = HashMap<String, Any>()
        hashMap.put("contentId", contentId)
        hashMap.put("contentUserId", contentUserId)
        hashMap.put("total", total)
        if (isEnding) {
            hashMap.put("isEnding", 1)
        }
        val mRequestBody: RequestBody = KukooHttpManager.createRequestBody(hashMap)

        return retrofitApi.saveListen(mRequestBody)
    }

}